﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;
using System.Data;
using System.Data.Common;

using Data_Objects;
using Business_Objects;
namespace Data_Objects.Implementation
{
    //Ko public -> tất cả gọi qua dataacess
    class SqlServerBlockDao : IBlockDao
    {
        public SqlServerBlockDao()
        {
            SQLUtility.TableName = "Block";
        }

        //Trả về đối tượng báo ăn
        private Block MakeBlock(DataRow row) 
        {
            Guid BlockID = Guid.Parse(row["BlockID"].ToString());
            string BlockNumber = row["BlockNumber"].ToString();
            string _name = row["BlockName"].ToString();
            int _schoolID = int.Parse(row["SchoolID"].ToString());
            return new Block(BlockID, BlockNumber, _name, _schoolID);
        }

        //Lấy danh sách khách hàng trả về datatable
        private IList<Block> MakeBlocks(DataTable dt)  
        {
            IList<Block> list = new List<Block>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(MakeBlock(row));
            }
            return list;
        }
        public IList<Business_Objects.Block> GetBlock()
        {

            SQLUtility.TableName = "Block";
            DbParameter[] param = null;
            DataTable dt = CommandProvider.GetDatatable(SQLUtility.ProceduresGetAll, param);
            return MakeBlocks(dt); 
        }

        public Business_Objects.Block GetBlockByID(string BlockID)
        {
            SQLUtility.TableName = "Block";
            DbParameter[] parameters = new DbParameter[1];
            parameters[0] = new SqlParameter("@BlockID", BlockID);
            DataRow row = CommandProvider.GetDataRow(SQLUtility.ProceduresGetByID, parameters);
            if (row == null) { return null; }
            else
            {
                return MakeBlock(row);
            }
        }

        public void InsertBlock(Business_Objects.Block Block)
        {
            SQLUtility.TableName = "Block";
            int newID = CommandProvider.Insert(SQLUtility.ProceduresInsert, Take(Block), true);
        }

        public int UpdateBlock(Business_Objects.Block Block)
        {
            SQLUtility.TableName = "Block";
            try { return CommandProvider.Update(SQLUtility.ProceduresUpdate, Take(Block)); }
            catch { return -1; }
        }

        public int DeleteBlock(Business_Objects.Block Block)
        {
            SQLUtility.TableName = "Block";
            try { return CommandProvider.Delete(SQLUtility.ProceduresDelete, Take(Block)); }
            catch { return -1; }
        }

        private DbParameter[] Take(Block Block)
        {
            DbParameter[] parameters = new DbParameter[4];
            parameters[0] = new SqlParameter("@BlockID", Block.Id);
            parameters[1] = new SqlParameter("@BlockNumber", Block.BlockNumber);
            parameters[2] = new SqlParameter("@BlockName", Block.Name);
            parameters[3] = new SqlParameter("@SchoolID", Block.SchoolID);
            return parameters;
        }


        public IList<Block> GetBlockBySchoolID(int schoolId)
        {          
            DbParameter[] parameters = new DbParameter[1];
            parameters[0] = new SqlParameter("@SchoolID", schoolId);
            DataTable dt = CommandProvider.GetDatatable("usp_SelectBlocksByAndSchoolID", parameters);
            return MakeBlocks(dt); 
        }


        public List<Block> GetBlockBySchoolIdAndSchoolYear(string SchoolYearID, int schoolId)
        {
            DbParameter[] parameters = new DbParameter[2];
            parameters[0] = new SqlParameter("@SchoolYearID", schoolId);
            parameters[1] = new SqlParameter("@SchoolID", schoolId);
            DataTable dt = CommandProvider.GetDatatable("usp_SelectTheoDoiThiDuasByAndSchoolYearIDAndSchoolID", parameters);
            return MakeBlocks(dt).ToList();  
        }
    }
}
